<?php

/**
 * This is the model class for table "tbl_service".
 *
 * The followings are the available columns in table 'tbl_service':
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property integer $servicetype
 * @property integer $money
 * @property string $description
 * @property string $contact
 * @property string $completiondate
 * @property string $keyword
 * @property string $image
 *
 * The followings are the available model relations:
 * @property Bidservice[] $bidservices
 * @property Comment[] $comments
 * @property Jobtype $servicetype0
 * @property Users $user
 * @property ServiceSkill[] $serviceSkills
 */
class Service extends CActiveRecord
{
    private $_oldTags;
    public $countBid;
        /**
         * Returns the static model of the specified AR class.
         * @param string $className active record class name.
         * @return Service the static model class
         */
        public static function model($className=__CLASS__)
        {
                return parent::model($className);
        }

        /**
         * @return string the associated database table name
         */
        public function tableName()
        {
                return 'tbl_service';
        }

        /**
         * @return array validation rules for model attributes.
         */
        
       public function rules()
        {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
            return array(
                array('name, servicetype, money, description, contact, completiondate, keyword, image', 'required'),
                array('user_id, servicetype,completiondate, service_contracted, service_deposited, service_completed, dnd_paid,', 'numerical', 'integerOnly'=>true),
                array('keyword', 'length', 'max'=>200),
                array('image', 'length', 'max'=>100),
                array('id, user_id, name, servicetype, money, description, contact, completiondate, keyword, image, time_create, time_update, service_contracted, service_deposited, service_completed, dnd_paid', 'safe', 'on'=>'search'),
                array('money','numerical',
                'integerOnly'=>true,
                'min'=>10000,
                'max'=>10000000,
                'tooSmall'=>'Sô tiền tối thiếu cho dịch vụ của bạn phải là 10 nghìn vnđ',
                'tooBig'=>'Số tiền tối đa cho dịch vụ của bạn là 10 triệu vnđ '),
                );
        }
        /**
         * @return array relational rules.
         */
        public function relations()
        {
                // NOTE: you may need to adjust the relation name and the related
                // class name for the relations automatically generated below.
                return array(
                        'bidservices' => array(self::HAS_MANY, 'Bidservice', 'service_id'),
                        'comments' => array(self::HAS_MANY, 'Comment', 'service_id'),
                        'servicetype' => array(self::BELONGS_TO, 'Jobtype', 'servicetype'),
                        'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
                        'serviceSkills' => array(self::HAS_MANY, 'ServiceSkill', 'service_id'),
                        
                );
        }

        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels()
        {
                return array(
                        'id' => 'ID',
                        'user_id' => 'User',
                        'name' => 'Tên',
                        'servicetype' => 'Loại',
                        'money' => 'Số tiền',
                        'description' => 'Mô tả',
                        'contact' => 'Liên hệ',
                        'completiondate' => 'Thực hiện',
                        'keyword' => 'Từ khóa',
                        'image' => 'ảnh',
                        'countBid'=>'Số người thuê dịch vụ',
                );
        }

        /**
         * Retrieves a list of models based on the current search/filter conditions.
         * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
         */
       	public function search()
    	{
    		// Warning: Please modify the following code to remove attributes that
    		// should not be searched.
    
    		$criteria=new CDbCriteria;
    
    		$criteria->compare('id',$this->id);
    		$criteria->compare('user_id',$this->user_id);
    		$criteria->compare('name',$this->name,true);
    		$criteria->compare('servicetype',$this->servicetype);
    		$criteria->compare('money',$this->money);
    		$criteria->compare('description',$this->description,true);
    		$criteria->compare('contact',$this->contact,true);
    		$criteria->compare('completiondate',$this->completiondate);
    		$criteria->compare('keyword',$this->keyword,true);
    		$criteria->compare('image',$this->image,true);
    		$criteria->compare('time_create',$this->time_create,true);
    		$criteria->compare('time_update',$this->time_update,true);
    		$criteria->compare('service_contracted',$this->service_contracted);
    		$criteria->compare('service_deposited',$this->service_deposited);
    		$criteria->compare('service_completed',$this->service_completed);
    		$criteria->compare('dnd_paid',$this->dnd_paid);
            
    
    		return new CActiveDataProvider($this, array(
    			'criteria'=>$criteria,
    		));
    	}
        
        public function search_user()
    	{
    		// Warning: Please modify the following code to remove attributes that
    		// should not be searched.
    
    		$criteria=new CDbCriteria;
            $criteria->condition='user_id='.Yii::app()->user->id;
    
    		$criteria->compare('id',$this->id);
    		$criteria->compare('user_id',$this->user_id);
    		$criteria->compare('name',$this->name,true);
    		$criteria->compare('servicetype',$this->servicetype);
    		$criteria->compare('money',$this->money);
    		$criteria->compare('description',$this->description,true);
    		$criteria->compare('contact',$this->contact,true);
    		$criteria->compare('completiondate',$this->completiondate);
    		$criteria->compare('keyword',$this->keyword,true);
    		$criteria->compare('image',$this->image,true);
    		$criteria->compare('time_create',$this->time_create,true);
    		$criteria->compare('time_update',$this->time_update,true);
    		$criteria->compare('service_contracted',$this->service_contracted);
    		$criteria->compare('service_deposited',$this->service_deposited);
    		$criteria->compare('service_completed',$this->service_completed);
    		$criteria->compare('dnd_paid',$this->dnd_paid);
            //$criteria->compare('countBid',$this->countBid);
    
    		return new CActiveDataProvider($this, array(
    			'criteria'=>$criteria,
                'pagination'=>array('pageSize'=>5),
    		));
    	}
        
        
    
    
    // Tag
    
    public function getUrl()
	{
		return Yii::app()->createUrl('/service/show/tag', array(
			'id'=>$this->id,
			'name'=>$this->name,
		));
	}

	/**
	 * @return array a list of links that point to the post list filtered by every tag of this post
	 */
	public function getTagLinks()
	{
		$links=array();
		foreach(Tag::string2array($this->keyword) as $tag)
			$links[]=CHtml::link(CHtml::encode($tag), array('/service/show/tag', 'tag'=>$tag));
		return $links;
	}
    
    /**
        Tách keyword thành t?ng ph?n t? cho vào m?ng
    */
   

	/**
	 * Normalizes the user-entered tags.
	 */
	public function normalizeTags($attribute,$params)
	{
		$this->keyword=Tag::array2string(array_unique(Tag::string2array($this->keyword)));
	}
	/**
	 * This is invoked when a record is populated with data from a find() call.
	 */
	
	protected function afterFind()
	{
		parent::afterFind();
		$this->_oldTags=$this->keyword;
	}

	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave()
	{
		parent::afterSave();
		Tag::model()->updateFrequency($this->_oldTags, $this->keyword);
	}

	/**
	 * This is invoked after the record is deleted.
	 */
	protected function afterDelete()
	{
		parent::afterDelete();
		Tag::model()->updateFrequency($this->keyword, '');
	}
}